/** FASSIT (Forms of Selective Attention in Intelligent Transportation Systems)
 * Computational Creativity and Digital Media
 * Cognitive and Media Systems Group
 * Centre for Informatics and Systems of the University of Coimbra (CISUC)
 *
 * Copyright (c) 2010-2013 University of Coimbra, Portugal
 * All rights reserved.
 */
package poi.SurpriseCalculator;

import java.util.ArrayList;
import java.util.HashMap;

import masterAgent.DBmanager.DBConnection;
import poi.Info.Day;
import poi.Info.DaysOn;
import poi.Info.POI;
import poi.Info.Schedule;
import poi.Surprise.SurpriseSchedule;
import agent.Context.Goal;

/**
 * This class is used to calculate the Master agent Surprise for the parameter
 * Schedule
 * 
 * @author Hernani Costa
 * @version 0.1 date: 29/11/2012
 */
public class SurpriseCalculatorForSchedule {
	HashMap<Integer, SurpriseSchedule> poiSurpriseSchedule;

	/**
	 * ------------------------------------------------------------------------
	 * Constructor class
	 * ------------------------------------------------------------------------
	 * 
	 * @param pois
	 *            - list with the POIs that we intend to calculate the surprise
	 */
	public SurpriseCalculatorForSchedule(HashMap<Integer,POI> pois) {
		poiSurpriseSchedule = new HashMap<Integer, SurpriseSchedule>();
		calculates_Surprise_Knowledge_About_Schedule(pois);
	}

	/**
	 * Calculates the Surprise for the parameter Schedule, by considering the average of the list of DaysOn 
	 * 
	 * @param pois
	 *            - list with the POIs that we intend to calculate the surprise
	 */
	private void calculates_Surprise_Knowledge_About_Schedule(
			HashMap<Integer,POI> pois) {
		SurpriseSchedule surpriseSchedule = null;
		//double val = 0.0;
		for (POI poi : pois.values()) {
			HashMap<String, Day> day = poi.getDaysOn().getListDaysOn();


			//String dayName = "";
			int[] sch = new int[3];
			int count =0;
			if (day.size() > 0) {
				for (String temp : day.keySet()) {
					Schedule s = day.get(temp).getSchedule();
					System.out.println("\t"+s.to_String() + " cout: "+s.getCount());
					count += s.getCount();
					sch[0] +=s.getSchedule()[0];
					sch[1] +=s.getSchedule()[1];
					sch[2] +=s.getSchedule()[2];
				}
			}
			System.out.println("\nCOUNT = "+count);
			System.out.println(sch[0]+ " "+sch[1]+ " " +sch[2]);
			
			
			surpriseSchedule = new SurpriseSchedule((double) sch[0]/count, (double) sch[1]/count, (double) sch[2]/count);
			System.out.println("\nNEW Surprise Schedule VALUES:\n\t\t "+ surpriseSchedule.to_String());
			
//			System.out.println("\nDAY NAME: "+ dayName);
//			Schedule schedule = day.get(dayName).getSchedule();
//			System.out.println("Schedule: "+schedule.to_String());
//			// schedule in an array, example: "Morning"=0, "Afternoon"=1,
//			// "Night"=1
//			int[] scheduleArray = schedule.getSchedule();
//			val = 1.0 / schedule.getCount();
//			System.out.println("VAL:" + val +" \t Count: "+schedule.getCount());
//			surpriseSchedule = new SurpriseSchedule(scheduleArray[0] * val,
//					scheduleArray[1] * val, scheduleArray[2] * val);

			poiSurpriseSchedule.put(poi.getPoiID(), surpriseSchedule);
		}
	}

	/**
	 * 
	 * @return a String with the Surprise values for the parameter Schedule, for
	 *         all POIs
	 */
	public String to_String() {
		String info = "";
		for (int poiID : poiSurpriseSchedule.keySet()) {
			info += "poiID: " + poiID + "\n\t"
					+ poiSurpriseSchedule.get(poiID).to_String() + "\n";
		}
		return info;
	}

	// ------------------------------------------------------------------------
	// ------------------------GETs and SETs-----------------------------------
	// ------------------------------------------------------------------------

	public HashMap<Integer, SurpriseSchedule> getPoiSurpriseSchedule() {
		return poiSurpriseSchedule;
	}

	/**
	 * ------------------------------------------------------------------------
	 * Only for debug purpose
	 * ------------------------------------------------------------------------
	 */
	public static void main(String args[]) {
		// DBManager manager = new DBManager();
		DBConnection connector = new DBConnection();

		POI poi = connector.getPOI(1507669);
		// System.err.println("O pesquisado\n" + poi.to_String() + "\n\n");

		HashMap<Integer,POI> pois = connector.getPOIsNearLocation(poi.getLocation(),
				200, new Goal().getCoffee());
		// for (POI p : pois) {
		// System.err.println(p.to_String());
		// }

		SurpriseCalculatorForSchedule m = new SurpriseCalculatorForSchedule(pois);
		System.out.println(m.to_String());
	}

}
